<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/pages/common/component.jsp"%>
<html>
<head>
<title>收债</title>

<script language="javascript">
var oprateAction = "creditDebtAction.do";

var creditDebtId = '<%=request.getParameter("creditDebtId")%>';

$(document).ready(function(){
	$.ajax({
	    url: oprateAction+'?method=findCreditDebt4Ajax&creditOrDebt=<%=Constants.CREDITDEBT_CREDIT%>&creditDebtId='+creditDebtId,
	    type: 'GET',
	    dataType: 'json',
	    cache: false,
	    error: function(){
	        alert('得到债权信息出错!');
	    },
	    success: function(json){
	    	var text = '债权人:'+json.creditor+',债务人:'+json.debtor+',剩余金额:'+accSub(json.amount,json.paidAmount)+',剩余利息:'+accSub(json.interest,json.paidInterest)
	    	$('#getcredit_main').html(text);
	    	// 表单赋值
	    	$("#paidAmount").val(accSub(json.amount,json.paidAmount));
	    	$("#paidInterest").val(accSub(json.interest,json.paidInterest));
	    }
	});
	
	// 填充账号信息select选项
	getAllAccount2Element('inAccountId', '');
	
});

function getCreditDebtAmount(){
	var paidAmount = $('#paidAmount').val();
	var paidInterest = $('#paidInterest').val();
	var inAccountId = $('#inAccountId').val();
	$.ajax({
	    url: oprateAction+'?method=getCreditDebtAmount&creditDebtId='+creditDebtId+'&paidAmount='+paidAmount+'&paidInterest='+paidInterest+'&inAccountId='+inAccountId,
	    type: 'GET',
	    dataType: 'text',
	    cache: false,
	    error: function(){
	        alert('更新债权信息出错!');
	    },
	    success: function(text){
	    	$("#getcredit_div").dialog("close");
	    	// 更新债权列表的值
	    	var list_paidAmount_value = $("#paidAmount_"+creditDebtId).html();
	    	var list_paidInterest_value = $("#paidInterest_"+creditDebtId).html();
	    	$("#paidAmount_"+creditDebtId).html(parseFloat(list_paidAmount_value) + parseFloat(paidAmount));
	    	$("#paidInterest_"+creditDebtId).html(parseFloat(list_paidInterest_value) + parseFloat(paidInterest));
	    	// 更新列显示:是否结账,操作
	    	var list_amount_value = $("#amount_"+creditDebtId).html();
	    	var list_interest_value = $("#interest_"+creditDebtId).html();
	    	// (已还款额+已还利息) - (借款额+借款利息) > 0 已结款
	    	if( (parseFloat($("#paidAmount_"+creditDebtId).html())+parseFloat($("#paidInterest_"+creditDebtId).html())) - (parseFloat(list_amount_value)+parseFloat(list_interest_value)) >= 0){ //已结款
	    		// 列显示置为已结款
	    		$("#isOver_"+creditDebtId).html("<font color=green>已完结</font>");
	    		// 还款日期置为 --
	    		$("#dateDeadline_"+creditDebtId).html("--");
	    		// 操作显示 编辑
	    		$("#oprate_"+creditDebtId).html("<a href=# onclick=editCredit('"+creditDebtId+"');>编辑</a>");
	    	}else{
	    		// 列显示置为未结款
	    		$("#isOver_"+creditDebtId).html("<font color=red>未完结</font>");
	    		// 操作显示 编辑,收款
	    		$("#oprate_"+creditDebtId).html("<a href=# onclick=editCredit('"+creditDebtId+"');>编辑</a>&nbsp;<a href=# onclick=getCreditFast('"+creditDebtId+"');>收款</a>");
	    	}
	    }
	});
}

</script>
</head>
<body leftmargin="8" topmargin="8" background='pages/desktop/skin/images/allbg.gif'>
<!--  内容列表   -->
<form id="getcreditForm" name="form2">
	<table width="98%" border="0" cellpadding="2" cellspacing="1" bgcolor="#D1DDAA" align="center" style="margin-top:8px">
		<tr>
			<td id="getcredit_main" height="24" colspan="11" background="pages/desktop/skin/images/tbg.gif">
				
			</td>
		</tr>
		<tr align='center' bgcolor="#FFFFFF" height="22" >
			<td>
				已收金额:
			</td>
			<td>
				<input class="validate[required]" id="paidAmount" name="paidAmount"/>
			</td>
		</tr>
		<tr align='center' bgcolor="#FFFFFF" height="22" >
			<td>
				已收利息:
			</td>
			<td>
				<input class="validate[required]" id="paidInterest" name="paidInterest"/>
			</td>
		</tr>
		<tr align='center' bgcolor="#FFFFFF" height="22" >
			<td>
				收入账号:
			</td>
			<td>
				<select id="inAccountId" name="inAccountId"></select>
			</td>
		</tr>
		<tr align='center' bgcolor="#FFFFFF" height="22" >
			<td colspan="2">
				<a href="javascript:getCreditDebtAmount()" class="coolbg">收款</a>
			</td>
		</tr>
	</table>
</form>
	
</body>
</html>